package com.ruoyi.business.domain.product;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.domain.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 商品SKU表实体类
 * 对应数据库表：tb_product_skus
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName(value = "tb_product_skus",autoResultMap = true)
@ApiModel(value = "TbProductSkus", description = "商品SKU表实体类")
public class TbProductSkus extends BaseEntity {

    /**
     * 商品sku主键id
     */
    @ApiModelProperty(value = "商品sku主键id")
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 商品ID
     */
    @ApiModelProperty(value = "商品ID")
    private Long productId;

    /**
     * SKU编码
     */
    @ApiModelProperty(value = "SKU编码")
    private String skuCode;

    /**
     * 条形码
     */
    @ApiModelProperty(value = "条形码")
    private String barcode;

    /**
     * 规格组合
     */
    @ApiModelProperty(value = "规格组合",example = "{\"颜色\":\"蓝色\",\"尺寸\":\"M\"}")
    @TableField(
        value = "specs",  // 对应数据库字段名
        typeHandler = JacksonTypeHandler.class  // 显式指定JSON类型处理器
    )
    private Map<String, String> specs;

    /**
     * 规格文本
     */
    @ApiModelProperty(value = "规格文本")
    private String specsText;

    /**
     * 售价
     * 固定金额需求：兑换1件需支付的金额，exchange_type=1时为0
     */
    @ApiModelProperty(value = "售价\n" +
        "固定金额需求：兑换1件需支付的金额，exchange_type=1时为0")
    private BigDecimal price;

    /**
     * 成本价
     */
    @ApiModelProperty(value = "成本价")
    private BigDecimal costPrice;

    /**
     * 原价
     */
    @ApiModelProperty(value = "原价")
    private BigDecimal originalPrice;

    /**
     * 库存
     */
    @ApiModelProperty(value = "库存")
    private Long stock;

    /**
     * 库存预警
     */
    @ApiModelProperty(value = "库存预警")
    private Long stockLowWarning;

    /**
     * SKU图片
     */
    @ApiModelProperty(value = "SKU图片")
    private String image;

    /**
     * 重量kg
     */
    @ApiModelProperty(value = "重量kg")
    private BigDecimal weight;

    /**
     * 体积
     */
    @ApiModelProperty(value = "体积")
    private BigDecimal volume;

    /**
     * 状态：1-有效 0-无效
     */
    @ApiModelProperty(value = "状态：1-有效 0-无效")
    private String status;

    @ApiModelProperty(value = "固定积分需求：兑换1件需消耗的积分，exchange_type=0时为0")
    private Long pointsNeeded;

    @ApiModelProperty(value = "单个用户积分兑换上限件：0-无上限")
    private Long pointsLimitPerUser;

    @ApiModelProperty(value = "积分兑换类型：0-仅纯金额购买，1-纯积分兑换，2-积分+金额兑换 3koc任务兑换卷")
    private Integer exchangeType;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间", readOnly = true)
    private Date createTime;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(value = "更新时间", readOnly = true)
    private Date updateTime;

    /**
     * 创建人
     */
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建人", readOnly = true)
    private String createBy;

    /**
     * 更新人
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(value = "更新人", readOnly = true)
    private String updateBy;

    public static String getSpecsText(Map<String, String> specs) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : specs.entrySet()) {
            sb.append(entry.getKey()).append(":").append(entry.getValue()).append(";");
        }
        return sb.toString();
    }
}
